package com.ruoyi.tongban.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tongban.domain.InsClassChild;
import com.ruoyi.tongban.mapper.InsClassChildMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tongban.mapper.InsChildMapper;
import com.ruoyi.tongban.domain.InsChild;
import com.ruoyi.tongban.service.IInsChildService;

/**
 * 孩子Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-21
 */
@Service
public class InsChildServiceImpl implements IInsChildService 
{
    @Autowired
    private InsChildMapper insChildMapper;

    @Autowired
    private InsClassChildMapper insClassChildMapper;

    /**
     * 查询孩子
     * 
     * @param childId 孩子ID
     * @return 孩子
     */
    @Override
    public InsChild selectInsChildById(Long childId)
    {
        return insChildMapper.selectInsChildById(childId);
    }

    /**
     * 查询孩子列表
     * @param childId 孩子id
     * @return 孩子集合
     */
    @Override
    public List<InsChild> institutionChild(Long childId) {
        return insChildMapper.listInstitutionChild(childId);
    }

    /**
     * 查询孩子列表
     * 
     * @param insChild 孩子
     * @return 孩子
     */
    @Override
    public List<InsChild> selectInsChildList(InsChild insChild)
    {
        return insChildMapper.selectInsChildList(insChild);
    }

    /**
     * 新增孩子
     * 
     * @param insChild 孩子
     * @return 结果
     */
    @Override
    public int insertInsChild(InsChild insChild)
    {
        if(insChild.getChildName() == null){
            throw new CustomException("添加失败,请添加孩子姓名!");
        }
        if(insChild.getChildSex() == null){
            throw new CustomException("添加失败,请选择孩子性别!");
        }
        if(insChild.getIsOilField() == null){
            throw new CustomException("添加失败,请选择油田类型!");
        }
        insChild.setStatus(1);
        insChild.setIsUpdate(0);
        insChild.setRecordStatus(1);
        insChild.setGuid(StringUtils.getUUID());
        insChild.setCreateTime(DateUtils.getNowDate());
        insChild.setJoinTime(DateUtils.getNowDate());
        insChild.setCreateUser(SecurityUtils.getLoginUser().getUser().getUserId());
        Integer resut = insChildMapper.insertInsChild(insChild);
        InsClassChild insClassChild = new InsClassChild();
        insClassChild.setChildId(insChild.getChildId());
        insClassChild.setClassId(insChild.getClassId());
        insClassChild.setStatus(1);
        insClassChild.setCreateTime(DateUtils.getNowDate());
        insClassChild.setCreateUser(SecurityUtils.getLoginUser().getUser().getUserId());
        insClassChildMapper.insertInsClassChild(insClassChild);
        return resut;
    }

    /**
     * 修改孩子
     * 
     * @param insChild 孩子
     * @return 结果
     */
    @Override
    public int updateInsChild(InsChild insChild)
    {
        if(insChild.getChildName() == null){
            throw new CustomException("添加失败,请添加孩子姓名!");
        }
        if(insChild.getChildSex() == null){
            throw new CustomException("添加失败,请选择孩子性别!");
        }
        if(insChild.getIsOilField() == null){
            throw new CustomException("添加失败,请选择油田类型!");
        }
        insChild.setUpdateUser(SecurityUtils.getLoginUser().getUser().getUserId());
        Integer resut = insChildMapper.updateInsChild(insChild);
        InsClassChild insClassChild = new InsClassChild();
        if(insClassChildMapper.selectInsClassChildByChildId(insChild.getChildId()) == null){
            insClassChild.setChildId(insChild.getChildId());
            insClassChild.setClassId(insChild.getClassId());
            insClassChild.setStatus(1);
            insClassChild.setCreateTime(DateUtils.getNowDate());
            insClassChild.setCreateUser(SecurityUtils.getLoginUser().getUser().getUserId());
            insClassChildMapper.insertInsClassChild(insClassChild);
        } else {
            insClassChild.setChildId(insChild.getChildId());
            insClassChild.setClassId(insChild.getClassId());
            insClassChild.setUpdateTime(DateUtils.getNowDate());
            insClassChild.setUpdateUser(SecurityUtils.getLoginUser().getUser().getUserId());
            insClassChildMapper.updateInsClassChild(insClassChild);
        }
        return resut;
    }

    /**
     * 批量删除孩子
     * 
     * @param childIds 需要删除的孩子ID
     * @return 结果
     */
    @Override
    public int deleteInsChildByIds(Long[] childIds)
    {
        return insChildMapper.deleteInsChildByIds(childIds);
    }

    /**
     * 删除孩子信息
     * 
     * @param childId 孩子ID
     * @return 结果
     */
    @Override
    public int deleteInsChildById(Long childId)
    {
        return insChildMapper.deleteInsChildById(childId);
    }
    
    public List<InsChild> selectChildSendTv(Map<String,  Object> paramMap){
        return insChildMapper.selectChildSendTv(paramMap);
    }
}
